<?php
namespace Admin\Controller;
use Think\Controller;
class SettingController extends Controller {
    //TP构造函数
    public function _initialize(){
        header("Content-type:text/html;charset=utf-8");
        //如果未登录，则跳转到登录页面
        if(!session('admin_user_id')&&!cookie('admin_user_id')){
            echo "<script>window.location.href='/admin/index/login'</script>";exit;
        }
    }
    //系统设置
    public function index(){
        $data['title']='环境信息 系统设置 - 找金银网';
        $info['php_os']=PHP_OS;
        $info['php_version']=PHP_VERSION;
        $this->assign("data", $data);
        $this->assign("info", $info);
        $this->display();
    }
    //环境信息
    public function site(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='站点设置 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //环境信息
    public function site_post(){
        $post=I('post.');
        if(!empty($_FILES['site_logo']['tmp_name'])){
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './data/upload/setting/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName=md5(time());
            $info   =   $upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $site_logo='/data/upload/setting/'.$info['site_logo']['savename'];
            $post['site_logo']=$site_logo;
        }
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/site';</script>";
    }
    //
    public function account(){
        $data['title']='账号同步 系统设置 - 找金银网';
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function account_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/account';</script>";
    }
    //
    public function upload(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='上传设置 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function upload_post(){
        $post=I('post.');
        if(!empty($_FILES['default_product_image']['tmp_name'])){
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './data/upload/setting/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName='product'.md5(time());
            $info   =   $upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $default_product_image='/data/upload/setting/'.$info['default_product_image']['savename'];
            $post['default_product_image']=$default_product_image;
        }
        if(!empty($_FILES['default_user_image']['tmp_name'])){
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './data/upload/setting/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName='user'.md5(time());
            $info   =   $upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $default_user_image='/data/upload/setting/'.$info['default_user_image']['savename'];
            $post['default_user_image']=$default_user_image;
        }
        if(!empty($_FILES['default_store_image']['tmp_name'])){
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './data/upload/setting/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName='store'.md5(time());
            $info   =   $upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $default_store_image='/data/upload/setting/'.$info['default_store_image']['savename'];
            $post['default_store_image']=$default_store_image;
        }
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/upload';</script>";
    }
    //
    public function seo(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='SEO设置 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function seo_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/seo';</script>";
    }
    //
    public function notice(){
        $data['title']='消息通知 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function message(){
        $data['title']='短信通知 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function pay(){
        /*$arr=array();
        $arr[0]['name']='支付宝账号';
        $arr[0]['value']='alipay';
        $arr[1]['name']='交易安全校验码（key）';
        $arr[1]['value']='alipay';
        $arr[2]['name']='合作者身份（partner ID）';
        $arr[2]['value']='alipay';
        $serialize=serialize($arr);
        echo $serialize;exit;*/
        $Model=D("Payment");
        $pay_list=$Model->get_pay_list();
        $data['title']='支付方式 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("pay_list", $pay_list);
        $this->display();
    }
    //
    public function pay_edit(){
        $data['title']='编辑支付方式 系统设置 - 找金银网';
        $pay_id=I('path.2');
        $Model=D("Payment");
        $Model->pay_id=$pay_id;
        $info=$Model->get_pay_info();
        $this->assign("data", $data);
        $this->assign("info", $info);
        $this->display();
    }
    //
    public function pay_edit_post(){
        $post=I('post.');
        //print_r($post);exit;
        $Model=D("Payment");
        $Model->post=$post;
        if(!$Model->pay_edit_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/pay';</script>";
    }
    //
    public function pay_add(){
        $data['title']='添加支付方式 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function pay_add_post(){
        $post=I('post.');
        $Model=D("Payment");
        $Model->post=$post;
        if(!$Model->pay_add_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/pay';</script>";
    }
    //删除及更新
    public function delete_pay()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Payment");
            $Model->ids=$ids;
            if(!$Model->delete_pay()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.pay_ids')){
            $pay_ids=I('post.pay_ids');
            $sorts=I('post.sorts');
            //print_r($sorts);exit;
            $Model=D("Payment");
            $Model->ids=$pay_ids;
            $Model->sorts=$sorts;
            if(!$Model->update_sorts()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //
    public function wechat(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='微信设置 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function wechat_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/wechat';</script>";
    }
    //
    public function search(){
        $data['title']='搜索设置 系统设置 - 找金银网';
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function search_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/search';</script>";
    }
    //
    public function language(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='语言设置 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function language_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/language';</script>";
    }
    //
    public function template(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='模板设置 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function template_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/template';</script>";
    }
    //
    public function cache(){
        $data['title']='清理缓存 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //清理缓存
    public function cache_post()
    {
        if(I('post.ids')){
            //echo CACHE_PATH;exit;
            $this->deldir(CACHE_PATH);
            echo "<script>alert('提交成功');window.location.href='/admin/setting/cache';</script>";
        }else{
            echo "<script>alert('未选择，请您选择后再重新提交');history.back(-1);</script>";exit;
        }
    }
    //删除文件
    private function deldir($dir) {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    deldir($fullpath);
                }
            }
        }
    }
    //
    public function optimization(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='性能优化 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function optimization_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/optimization';</script>";
    }
    //
    public function authority(){
        $data['title']='管理权限 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function log(){
        $data['title']='操作日志 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function data(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='数据维护 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function data_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/data';</script>";
    }
    //
    public function upgrade(){
        $Model=D("Setting");
        $setting_info=$Model->get_setting_info();
        $data['title']='系统更新 系统设置 - 找金银网';
        $this->assign("data", $data);
        $this->assign("setting_info", $setting_info);
        $this->display();
    }
    //
    public function upgrade_post(){
        $post=I('post.');
        $Model=D("Setting");
        $Model->post=$post;
        if(!$Model->setting_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/setting/upgrade';</script>";
    }

}